/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

/**
 * This page allows to get the following information in order to create the new plan
 *  - plan name
 *  - priority
 *  - type
 *  
 *  @author Pablo Muñoz
 */

public class AddPlanWizardPage2 extends WizardPage {
	private Text nameText;

	private Spinner prioritySpinner;
	
	private Button enablePriority;
	
	private Combo typeCombo;
	
	private Button enableType;

	/**
	 * Constructor for AddWizardPage2
	 * 
	 * @param pageName
	 */
	public AddPlanWizardPage2(ISelection selection) {
		super("AddPlanWizardPage2");
		setTitle("Add Plan Wizard");
		setDescription("Page 2: Plan details");
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 3;
		layout.horizontalSpacing = 50;
		layout.verticalSpacing = 9;
		
		Label label1 = new Label(container, SWT.NULL);
		label1.setText("Name:");
		nameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		nameText.setLayoutData(gd);
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText("&Priority (optional):");
		gd = new GridData(GridData.FILL_HORIZONTAL);
		prioritySpinner = new Spinner (container, SWT.BORDER);
		prioritySpinner.setIncrement(1);
		prioritySpinner.setPageIncrement(1);
		prioritySpinner.setSelection(0);
		prioritySpinner.setLayoutData(gd);
		prioritySpinner.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		enablePriority = new Button (container, SWT.CHECK);
		enablePriority.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		separator.setLayoutData(gd);
		gd.horizontalSpan = 3;
		
		Label label6 = new Label(container, SWT.NULL);
		label6.setText("&Type (optional):");
		typeCombo = new Combo(container, SWT.READ_ONLY);
		typeCombo.setItems (new String [] {"standard", "bpmn"});
		typeCombo.select(0);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		typeCombo.setLayoutData(gd);
		typeCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		enableType = new Button (container, SWT.CHECK);
		enableType.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		initialize();
		dialogChanged();
		setControl(container);
	}

	/*
	 * This method is called when any 'enable' checkbox is checked
	 */
	private void initialize() {
		enabledPropertiesChanged();
	}

	/*
	 * Ensures that all text fields are set.
	 */
	private void dialogChanged() {
		if(nameText.getText().length() == 0){
			updateStatus("A name must be specified for the new Plan");
			return;
		}
		updateStatus(null);
	}
	
	
	/*
	 * This method is called when any 'enable' checkbox is checked
	 */
	private void enabledPropertiesChanged() {
		prioritySpinner.setEnabled(enablePriority.getSelection());
		typeCombo.setEnabled(enableType.getSelection());
	}

	/*
	 * This method is called in order to update the status message 
	 */
	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	/**
	 * 
	 * @return plan name
	 */
	public String getPlanName(){
		return nameText.getText();
	}
	
	/**
	 * 
	 * @return plan priority
	 */
	public String getPlanPriority(){
		if(prioritySpinner.isEnabled()){
			return prioritySpinner.getText();
		}
		else{
			return null;
		}
	}
	
	/**
	 * 
	 * @return plan type
	 */
	public String getPlanType(){
		if(typeCombo.isEnabled()){
			return typeCombo.getText();
		}
		else{
			return null;
		}
	}
}